import pyefun as efun
import win32com.client as 对象
import win32api, pythoncom
import multiprocessing
import win32com.client, os, time, win32com, pythoncom, threading, pyefun, asyncio
class ExcelEvents:
    def __init__(self):
        print("initExcelEvent")
        pass

    def __enter__(self):
        print("enter")
        pass

    def __del__(self):
        print("del")
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("exit")
        pass

    def OnSheetSelectionChange(self, *args):
        print(args[1].Address)
        if args[1].Value == "关闭":
            print("关闭1")
            win32api.PostQuitMessage()  # 点击内容位关闭的单元格即可退出事件循环，所有事件无效。
            print("关闭2")

    def OnWorkbookNewSheet(self, *args):
        print("OnWorkbookNewSheet")
class ExcelOperate:
    def __init__(self):
        try:
            self.excelObj = 对象_获取("Excel.Application")
            self.method = "获取方式"
        except:
            self.excelObj = 对象_创建("Excel.Application")
            self.method = "创建方式"
            pass
        self.excelObj.Visible = True
        self.excelObjWithEvents = 对象_获取对象带事件(self.excelObj, ExcelEvents)
        pass
    def __enter__(self):
        print("enter_ope")
        return self
        pass
    def __del__(self):
        print("del_ope")
        pass
    def __exit__(self, exc_type, exc_val, exc_tb):
        print("exit_ope")
        pass
    def open(self,path):
        self.excelObj.Workbooks.Open(path)
        # self.excelObj.Workbooks.Open(r"D:\\d1.xlsx")
        pass
    def workbook(self, item):
        return self.excelObj.Workbooks(item)
    def worksheet(self, item_wb, item_sh):
        return self.workbook(item_wb).Worksheets(item_sh)
        pass  # def
    def cells_read(self, item_wb, item_sh, item_row, item_col):
        return self.workbook(item_wb).Worksheets(item_sh).Cells(item_row, item_col).Value
    def cells_write(self, item_wb, item_sh, item_row, item_col, value):
        self.workbook(item_wb).Worksheets(item_sh).Cells(item_row, item_col).Value = value
        pass
    def range_read(self, item_wb, item_sh, address):
        '''返回一个区域，二维数组对象'''
        return self.workbook(item_wb).Worksheets(item_sh).Range(address)
        pass
    def range_write(self, item_wb, item_sh, address, value):
        '''value可以是数组，数字，字符 下面的例子是C1到CX100写入一万个数字，执行效率非常高0.3秒,addrss可以是单元格也可以是 区域'''
        # self.excelObj.Range("C1:CX100").Value = [ [i] *100 for i in range(1,101)]
        # print(self.excelObj.Range("C1:CX100").Count)
        self.workbook(item_wb).Worksheets(item_sh).Range(address).Value = value
def 对象_获取(对象名):
    return 对象.GetActiveObject(对象名)
def 对象_获取对象带事件(对象名, 类名):
    return 对象.DispatchWithEvents(对象名, 类名)
def 对象_创建(对象名):
    return 对象.Dispatch(对象名)
def 多进程com创建excel():#测试的例子
    '''测试的函数'''
   # efun.线程_初始化COM库() #多进程方式不需要此命令
    with ExcelOperate() as eo:
        print(eo.method)
        if eo.method == "创建方式":
            eo.open("D:\\d1.xlsx")
        eo.cells_write(1, 1, 3, 3, "python hi  ")
        eo.range_write(1, 1, "A1:C3", ["a", 'b', "d"])
        pass
    pythoncom.PumpMessages()
    # efun.线程_取消COM库()
if __name__ == "__main__":
    p1 = multiprocessing.Process(target=多进程com创建excel)
    # p1 =multiprocessing.Process(target=多进程com创建excel()) #此中方法会出BUG ，禁止使用，正常用上面的方法
    p1.start()



